package com.zxh.blog.web;


import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zxh
 * @Title: test
 * @ProjectName blog
 * @Description: TODO
 * @date 2019/2/1416:46
 */
public class Test implements  Runnable{

    private AtomicInteger num = new AtomicInteger(0); // 出票数
    private AtomicInteger count = new AtomicInteger(900000000);; // 剩余票数

    private int num1 = 0; // 出票数
    private int count1 = 100000;; // 剩余票数

    boolean flag = false;

    @Override
    public void run() {
        while (true) {

            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int cnt = count.get();
                if(cnt >=0){
                   cnt= count.decrementAndGet();
                   if(cnt >=0){
                       System.out.println(Thread.currentThread().getName() + "买票,还剩" + cnt);
                   }else{
                       System.out.println(Thread.currentThread().getName() + "没有买到票");
                   }
                }else {
                     System.out.println(Thread.currentThread().getName() + "没有买到票");
                    Thread.currentThread().stop();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
       /* while (true) {

            // 没有余票时，跳出循环
            if (count1 <= 0) {
                break;
            }
            num1++;
            count1--;
            System.out.println("显示出票信息：" + Thread.currentThread().getName()
                    + "抢到第" + num1 + "张票，剩余" + count1 + "张票");
            try {
                Thread.sleep(500);// 模拟网络延时
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }*/
    }

}
